﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCSLibrary;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;
using Utils.Entity;

public partial class Admin_Form_News_Add : System.Web.UI.Page
{
    FunctionPageUI objFunction = new FunctionPageUI();
    NewsUI objNews = new NewsUI();
    DataSet dsCategoryPage;
    String newsType = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objFunction.GetAll();

        if (Request.QueryString["type"] != null)
        {
            newsType = Request.QueryString["type"];
        }

        if (!IsPostBack)
        {
            //BindCategoryPage();

            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtNewsTitle.Text.Trim());
        if (txtNewsID.Value.Trim() == "")
        {
            try
            {
                int result = objNews.Add(Convert.ToInt32(cboCategoryPage.SelectedValue), txtMetaKeyword.Text, txtMetaDescription.Text, txtNewsTitle.Text, alias,
                    txtNewsAbstract.Text, txtNewsContent.Content, txtNewsImage.Text, txtNewsSource.Text,"",0,Convert.ToInt32(txtViewOrder.Text),
                    chkIsHot.Checked, chkIsTypical.Checked, chkIsNew.Checked, false, ((UserEntity)Session[PageUtility.SESSION_USER_NAME]).UserID, DateTime.Now, ((UserEntity)Session[PageUtility.SESSION_USER_NAME]).UserID, "", "", newsType);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                //lMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objNews.Update(Convert.ToInt32(txtNewsID.Value), Convert.ToInt32(cboCategoryPage.SelectedValue), txtMetaKeyword.Text, txtMetaDescription.Text, txtNewsTitle.Text, alias,
                    txtNewsAbstract.Text, txtNewsContent.Content, txtNewsImage.Text, txtNewsSource.Text, "",Convert.ToInt32(txtViewOrder.Text),
                    chkIsHot.Checked, chkIsTypical.Checked, chkIsNew.Checked,"", "", newsType);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-noi-dung/quan-tri-bai-viet-chia-se.aspx");
    }

    #region Cac ham tuong tac du lieu

    private void BindCategoryPage()
    {
        try
        {
            string filter = "";
            if (newsType == "News")
            {
                filter = "FunctionPageAlias = 'tin-tuc'";
            }
            if (newsType == "News")
            {
                filter = "FunctionPageAlias = 'tin-tuc'";
            }
            else if(newsType == "Introduce")
            {
                filter = "FunctionPageAlias = 'gioi-thieu'";
            }
            txtCategoryPageID.Value = TableLib.CreateTable(dsCategoryPage.Tables[0], filter).Rows[0]["FunctionPageID"].ToString();
        }
        catch (Exception e)
        {

        }
    }

    private void LoadEditData(int NewsID)
    {
        DataSet ds = objNews.GetByID(NewsID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtNewsID.Value = row["NewsID"].ToString();
            
            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtNewsContent.Content = row["NewsContent"].ToString();
            txtNewsImage.Text = row["NewsImage"].ToString();
            txtNewsSource.Text = row["NewsSource"].ToString();
            txtNewsTitle.Text = row["NewsTitle"].ToString();
            txtNewsAbstract.Text = row["NewsAbstract"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();
            chkIsHot.Checked = Convert.ToBoolean(row["IsHot"]);
            chkIsNew.Checked = Convert.ToBoolean(row["IsNew"]);
            chkIsTypical.Checked = Convert.ToBoolean(row["IsTypical"]);
            btnCreate.Text = "Cập nhật";
            General.SetSelectedItemInComboBox(row["CategoryPageID"].ToString(), cboCategoryPage);
        }
    }

    private void ReInsertData()
    {
        //txtNewsID.Value = "";

        txtMetaDescription.Text = "";
        txtMetaKeyword.Text = "";
        txtNewsContent.Content = "";
        txtNewsImage.Text = "";
        txtNewsSource.Text = "";
        txtNewsTitle.Text = "";
        txtNewsAbstract.Text = "";
        txtViewOrder.Text = "";
        chkIsHot.Checked = false;
        chkIsNew.Checked = false;
        chkIsTypical.Checked = false;

        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    #endregion
}
